﻿using System;
using System.Windows.Forms;
using BusinessEntities;
using BusinessLayer;
using Draco.Algorithms;
using Draco.DataStructure;

namespace WindowsFormsApp
{
    public partial class Client : Form
    {
        private MetroGraph metroGraph;
        private GrogshopBLL grogshopBll;
        private RestaurantBLL restaurantBll;
        private SupermarketBLL supermarketBll;

        private D_SeqList<ILocatingPoint> siteList;  //所有内容
        private D_LinkList<Grogshop> nearbyGrogshop;
        private D_LinkList<Restaurant> nearbyRestaurant;
        private D_LinkList<Supermarket> nearbySupermarket;
        private D_LinkList<MetroNode> nearbyMetroNode;
        private D_LinkList<ILocatingPoint> nearbySites;   //附近信息现实的所有内容
        private ILocatingPoint targetSite;   //查询附近信息的地点

        private D_LinkList<ILocatingPoint> bgSites; //后台管理显示的所有内容
        private D_LinkQueue<int> indexs; //后台管理字符串搜索显示顺序
        private string preSeekStr = ""; //前一个搜索字符串

        private bool graphChanged = false; //判断图是否改变

        public Client()
        {
            InitializeComponent();
        }

        private void Client_Load(object sender, EventArgs e)
        {
            metroGraph = metroGraphView.Graph;

            grogshopBll = new GrogshopBLL();
            restaurantBll = new RestaurantBLL();
            supermarketBll = new SupermarketBLL(); ;

            int siteCount = metroGraph.Nodes.Count + grogshopBll.Grogshops.Count + restaurantBll.Restaurants.Count + supermarketBll.Supermarkets.Count;

            siteList = new D_SeqList<ILocatingPoint>(siteCount);

            foreach (MetroNode node in metroGraph.Nodes)
            {
                siteList.Add(node);
            }
            foreach (Grogshop grogshop in grogshopBll.Grogshops)
            {
                siteList.Add(grogshop);
            }
            foreach (Restaurant restaurant in restaurantBll.Restaurants)
            {
                siteList.Add(restaurant);
            }
            foreach (Supermarket supermarket in supermarketBll.Supermarkets)
            {
                siteList.Add(supermarket);
            }

            InputSuggest();
        }

        //文本框输入提示  
        private void InputSuggest()
        {
            AutoCompleteStringCollection nodeStrs = new AutoCompleteStringCollection();

            foreach (MetroNode node in metroGraph.Nodes)
            {
                nodeStrs.Add(node.Name);
            }

            txt_start.AutoCompleteCustomSource = nodeStrs;
            txt_start.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txt_start.AutoCompleteMode = AutoCompleteMode.Suggest;
            txt_end.AutoCompleteCustomSource = nodeStrs;
            txt_end.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txt_end.AutoCompleteMode = AutoCompleteMode.Suggest;


            AutoCompleteStringCollection allStrs = new AutoCompleteStringCollection();

            foreach (ILocatingPoint site in siteList)
            {
                allStrs.Add(site.Name);
            }

            txt_site.AutoCompleteCustomSource = allStrs;
            txt_site.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txt_site.AutoCompleteMode = AutoCompleteMode.Suggest;
        }

        private void btn_findpath_Click(object sender, EventArgs e)
        {
            if (txt_start.Text == "" || txt_end.Text == "")
            {
                MessageBox.Show("请输入起始站和终点站");
                return;
            }


            SeekPathOption spo = SeekPathOption.LeastTransfer;
            switch (cbo_seekOption.SelectedItem as string)
            {
                case "换乘最少":
                    spo = SeekPathOption.LeastTransfer;
                    break;
                case "距离最短":
                    spo = SeekPathOption.Shortest;
                    break;
                case "票价最低":
                    spo = SeekPathOption.Cheapest;
                    break;
                case "时间最少":
                    spo = SeekPathOption.LeastTime;
                    break;
            }
            MetroPath path = metroGraph.FindPath(txt_start.Text, txt_end.Text, spo);

            metroGraphView.SetCurPath(path);

            lbl_transfer.Text = path.Transfers + "次";
            lbl_distance.Text = path.GetDistance() + "米";
            lbl_usedTime.Text = path.GetTime() + "分";
            lbl_price.Text = path.GetPrice() + "元";



            lvw_plan.BeginUpdate();
            lvw_plan.Items.Clear();

            MetroNode startNode = path.Links[0].From;
            lvw_AddItem(lvw_plan, startNode.Name, "起始站");

            for (int i = 0; i < path.Links.Count; i++)
            {
                MetroNode node = path.Links[i].To;

                if (i == path.Links.Count - 1)
                {
                    lvw_AddItem(lvw_plan, node.Name, "终点站");
                }
                else if (node.Lines.Count > 1 && path.Links[i + 1].LineID != path.Links[i].LineID)
                {
                    lvw_AddItem(lvw_plan, node.Name, "换乘 " + path.Links[i].LineID + "号线->" + path.Links[i + 1].LineID + "号线");
                }
                else
                {
                    lvw_AddItem(lvw_plan, node.Name);
                }
            }

            lvw_plan.EndUpdate();

        }

        private void btn_clearpath_Click(object sender, EventArgs e)
        {
            metroGraphView.SetCurPath(null);

            lbl_transfer.Text = "";
            lbl_distance.Text = "";
            lbl_usedTime.Text = "";
            lbl_price.Text = "";

            lvw_plan.Items.Clear();
        }

        private void btn_findinfo_Click(object sender, EventArgs e)
        {
            if (txt_site.Text == "") return;

            nearbyGrogshop = new D_LinkList<Grogshop>();
            nearbyRestaurant = new D_LinkList<Restaurant>();
            nearbySupermarket = new D_LinkList<Supermarket>();
            nearbyMetroNode = new D_LinkList<MetroNode>();
            nearbySites = new D_LinkList<ILocatingPoint>();

            SeekAdmissibleError admissibleError = SeekAdmissibleError.Level_5;

            foreach (ILocatingPoint site in siteList)
            {
                if (D_String.PrefixMatching(site.Name, txt_site.Text))
                {
                    targetSite = site;

                    #region 获得附近信息

                    if (chk_grogshop.Checked)
                    {
                        nearbyGrogshop = grogshopBll.GetNearbySites(site.X, site.Y, admissibleError);
                    }
                    if (chk_restaurant.Checked)
                    {
                        nearbyRestaurant = restaurantBll.GetNearbySites(site.X, site.Y, admissibleError);
                    }
                    if (chk_supermarket.Checked)
                    {
                        nearbySupermarket = supermarketBll.GetNearbySites(site.X, site.Y, admissibleError);
                    }
                    if (chk_metronode.Checked)
                    {
                        nearbyMetroNode = metroGraph.GetNearbySites(site.X, site.Y, admissibleError);
                    }

                    #endregion

                    #region 显示附近信息

                    lvw_info.BeginUpdate();
                    lvw_info.Items.Clear();

                    foreach (Grogshop grogshop in nearbyGrogshop)
                    {
                        if (grogshop.Name == site.Name) continue;
                        lvw_AddItem(lvw_info, grogshop.Name, (int)D_Vector2D.Distance(site.X, site.Y, grogshop.X, grogshop.Y) + "米", grogshop.Grade + "分");
                        nearbySites.Add(grogshop);
                    }
                    foreach (Restaurant restaurant in nearbyRestaurant)
                    {
                        if (restaurant.Name == site.Name) continue;
                        lvw_AddItem(lvw_info, restaurant.Name, (int)D_Vector2D.Distance(site.X, site.Y, restaurant.X, restaurant.Y) + "米", restaurant.Grade + "分");
                        nearbySites.Add(restaurant);
                    }
                    foreach (Supermarket supermarket in nearbySupermarket)
                    {
                        if (supermarket.Name == site.Name) continue;
                        lvw_AddItem(lvw_info, supermarket.Name, (int)D_Vector2D.Distance(site.X, site.Y, supermarket.X, supermarket.Y) + "米", supermarket.Grade + "分");
                        nearbySites.Add(supermarket);
                    }
                    foreach (MetroNode node in nearbyMetroNode)
                    {
                        if (node.Name == site.Name) continue;
                        lvw_AddItem(lvw_info, node.Name, (int)D_Vector2D.Distance(site.X, site.Y, node.X, node.Y) + "米", "地铁站");
                        nearbySites.Add(node);
                    }

                    lvw_info.EndUpdate();

                    #endregion

                    return;
                }
            }
            MessageBox.Show("没有找到指定地点", "提示");
        }

        private void btn_clearinfo_Click(object sender, EventArgs e)
        {
            lvw_info.Items.Clear();
        }

        private void lvw_AddItem(ListView lvw, params string[] txts)
        {
            ListViewItem item = lvw.Items.Add(lvw.Items.Count + 1 + "");
            foreach (string s in txts)
            {
                item.SubItems.Add(s);
            }
        }

        private void lvw_info_SelectedIndexChanged(object sender, EventArgs e)
        {
            //显示被选择的地点
            //if (lvw_info.FocusedItem != null)
            //{
            //    MessageBox.Show(lvw_info.FocusedItem.SubItems[1].Text);
            //}

            D_LinkList<string> selectedStr = new D_LinkList<string>();
            D_LinkList<ILocatingPoint> targetSite = new D_LinkList<ILocatingPoint>();

            foreach (ListViewItem item in lvw_info.SelectedItems)
            {
                selectedStr.Add(item.SubItems[1].Text);
            }

            foreach (ILocatingPoint site in nearbySites)
            {
                foreach (string s in selectedStr)
                {
                    if (site.Name == s)
                    {
                        targetSite.Add(site);
                    }
                }
            }

            metroGraphView.SetSites(targetSite);
        }

        private void lvw_info_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (targetSite == null) return;

            lvw_info.BeginUpdate();
            lvw_info.Items.Clear();

            switch (e.Column)
            {
                case 0://序号
                    break;
                case 1://名称

                    #region 逐类型排序

                    string[] names;
                    int[] nameIndex;

                    //酒店排序
                    names = new string[nearbyGrogshop.Count];
                    for (int i = 0; i < nearbyGrogshop.Count; i++)
                    {
                        names[i] = nearbyGrogshop[i].Name;
                    }
                    nameIndex = D_Sort.QuickSortIndex(names);
                    for (int i = 0; i < nameIndex.Length; i++)
                    {
                        if (nearbyGrogshop[nameIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbyGrogshop[nameIndex[i]].Name, (int)D_Vector2D.Distance(nearbyGrogshop[nameIndex[i]].X, nearbyGrogshop[nameIndex[i]].Y, targetSite.X, targetSite.Y) + "米", nearbyGrogshop[nameIndex[i]].Grade + "分");
                    }

                    //美食排序
                    names = new string[nearbyRestaurant.Count];
                    for (int i = 0; i < nearbyRestaurant.Count; i++)
                    {
                        names[i] = nearbyRestaurant[i].Name;
                    }
                    nameIndex = D_Sort.QuickSortIndex(names);
                    for (int i = 0; i < nameIndex.Length; i++)
                    {
                        if (nearbyRestaurant[nameIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbyRestaurant[nameIndex[i]].Name, (int)D_Vector2D.Distance(nearbyRestaurant[nameIndex[i]].X, nearbyRestaurant[nameIndex[i]].Y, targetSite.X, targetSite.Y) + "米", nearbyRestaurant[nameIndex[i]].Grade + "分");
                    }

                    //超市排序
                    names = new string[nearbySupermarket.Count];
                    for (int i = 0; i < nearbySupermarket.Count; i++)
                    {
                        names[i] = nearbySupermarket[i].Name;
                    }
                    nameIndex = D_Sort.QuickSortIndex(names);
                    for (int i = 0; i < nameIndex.Length; i++)
                    {
                        if (nearbySupermarket[nameIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbySupermarket[nameIndex[i]].Name, (int)D_Vector2D.Distance(nearbySupermarket[nameIndex[i]].X, nearbySupermarket[nameIndex[i]].Y, targetSite.X, targetSite.Y) + "米", nearbySupermarket[nameIndex[i]].Grade + "分");
                    }

                    //站点排序
                    names = new string[nearbyMetroNode.Count];
                    for (int i = 0; i < nearbyMetroNode.Count; i++)
                    {
                        names[i] = nearbyMetroNode[i].Name;
                    }
                    nameIndex = D_Sort.QuickSortIndex(names);
                    for (int i = 0; i < nameIndex.Length; i++)
                    {
                        if (nearbyMetroNode[nameIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbyMetroNode[nameIndex[i]].Name, (int)D_Vector2D.Distance(nearbyMetroNode[nameIndex[i]].X, nearbyMetroNode[nameIndex[i]].Y, targetSite.X, targetSite.Y) + "米", "地铁站");
                    }

                    #endregion

                    break;
                case 2://距离

                    #region 逐类型排序

                    int[] distances, disIndex;

                    //酒店排序
                    distances = new int[nearbyGrogshop.Count];
                    for (int i = 0; i < nearbyGrogshop.Count; i++)
                    {
                        distances[i] = (int)D_Vector2D.Distance(nearbyGrogshop[i].X, nearbyGrogshop[i].Y, targetSite.X, targetSite.Y);
                    }
                    disIndex = D_Sort.QuickSortIndex(distances);
                    for (int i = 0; i < disIndex.Length; i++)
                    {
                        if (nearbyGrogshop[disIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbyGrogshop[disIndex[i]].Name, distances[disIndex[i]] + "米", nearbyGrogshop[disIndex[i]].Grade + "分");
                    }

                    //美食排序
                    distances = new int[nearbyRestaurant.Count];
                    for (int i = 0; i < nearbyRestaurant.Count; i++)
                    {
                        distances[i] = (int)D_Vector2D.Distance(nearbyRestaurant[i].X, nearbyRestaurant[i].Y, targetSite.X, targetSite.Y);
                    }
                    disIndex = D_Sort.QuickSortIndex(distances);
                    for (int i = 0; i < disIndex.Length; i++)
                    {
                        if (nearbyRestaurant[disIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbyRestaurant[disIndex[i]].Name, distances[disIndex[i]] + "米", nearbyRestaurant[disIndex[i]].Grade + "分");
                    }

                    //超市排序
                    distances = new int[nearbySupermarket.Count];
                    for (int i = 0; i < nearbySupermarket.Count; i++)
                    {
                        distances[i] = (int)D_Vector2D.Distance(nearbySupermarket[i].X, nearbySupermarket[i].Y, targetSite.X, targetSite.Y);
                    }
                    disIndex = D_Sort.QuickSortIndex(distances);
                    for (int i = 0; i < disIndex.Length; i++)
                    {
                        if (nearbySupermarket[disIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbySupermarket[disIndex[i]].Name, distances[disIndex[i]] + "米", nearbySupermarket[disIndex[i]].Grade + "分");
                    }

                    //站点排序
                    distances = new int[nearbyMetroNode.Count];
                    for (int i = 0; i < nearbyMetroNode.Count; i++)
                    {
                        distances[i] = (int)D_Vector2D.Distance(nearbyMetroNode[i].X, nearbyMetroNode[i].Y, targetSite.X, targetSite.Y);
                    }
                    disIndex = D_Sort.QuickSortIndex(distances);
                    for (int i = 0; i < disIndex.Length; i++)
                    {
                        if (nearbyMetroNode[disIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbyMetroNode[disIndex[i]].Name, distances[disIndex[i]] + "米", "地铁站");
                    }

                    #endregion

                    break;
                case 3://评分

                    #region 逐类型排序

                    int[] grades, gradeIndex;

                    //酒店排序
                    grades = new int[nearbyGrogshop.Count];
                    for (int i = 0; i < nearbyGrogshop.Count; i++)
                    {
                        grades[i] = nearbyGrogshop[i].Grade;
                    }
                    gradeIndex = D_Sort.QuickSortIndex(grades);
                    for (int i = gradeIndex.Length - 1; i >= 0; i--)
                    {
                        if (nearbyGrogshop[gradeIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbyGrogshop[gradeIndex[i]].Name, (int)D_Vector2D.Distance(nearbyGrogshop[gradeIndex[i]].X, nearbyGrogshop[gradeIndex[i]].Y, targetSite.X, targetSite.Y) + "米", nearbyGrogshop[gradeIndex[i]].Grade + "分");
                    }

                    //美食排序
                    grades = new int[nearbyRestaurant.Count];
                    for (int i = 0; i < nearbyRestaurant.Count; i++)
                    {
                        grades[i] = nearbyRestaurant[i].Grade;
                    }
                    gradeIndex = D_Sort.QuickSortIndex(grades);
                    for (int i = gradeIndex.Length - 1; i >= 0; i--)
                    {
                        if (nearbyRestaurant[gradeIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbyRestaurant[gradeIndex[i]].Name, (int)D_Vector2D.Distance(nearbyRestaurant[gradeIndex[i]].X, nearbyRestaurant[gradeIndex[i]].Y, targetSite.X, targetSite.Y) + "米", nearbyRestaurant[gradeIndex[i]].Grade + "分");
                    }

                    //超市排序
                    grades = new int[nearbySupermarket.Count];
                    for (int i = 0; i < nearbySupermarket.Count; i++)
                    {
                        grades[i] = nearbySupermarket[i].Grade;
                    }
                    gradeIndex = D_Sort.QuickSortIndex(grades);
                    for (int i = gradeIndex.Length - 1; i >= 0; i--)
                    {
                        if (nearbySupermarket[gradeIndex[i]].Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, nearbySupermarket[gradeIndex[i]].Name, (int)D_Vector2D.Distance(nearbySupermarket[gradeIndex[i]].X, nearbySupermarket[gradeIndex[i]].Y, targetSite.X, targetSite.Y) + "米", nearbySupermarket[gradeIndex[i]].Grade + "分");
                    }

                    //站点不排序
                    foreach (MetroNode node in nearbyMetroNode)
                    {
                        if (node.Name == targetSite.Name) continue;
                        lvw_AddItem(lvw_info, node.Name, (int)D_Vector2D.Distance(node.X, node.Y, targetSite.X, targetSite.Y) + "米", "地铁站");
                    }

                    #endregion

                    break;
            }

            lvw_info.EndUpdate();
        }

        private void lvw_info_Leave(object sender, EventArgs e)
        {
            metroGraphView.SetSites(null);
        }

        private void rdo_grogshop_CheckedChanged(object sender, EventArgs e)
        {
            if (rdo_grogshop.Checked)
            {
                //构架表头
                lvw_background.Columns.Clear();
                lvw_background.Items.Clear();
                ColumnHeader h1 = new ColumnHeader();
                h1.Text = "ID";
                h1.Width = 40;
                ColumnHeader h2 = new ColumnHeader();
                h2.Text = "名称";
                h2.Width = 80;
                ColumnHeader h3 = new ColumnHeader();
                h3.Text = "位置";
                h3.Width = 80;
                ColumnHeader h4 = new ColumnHeader();
                h4.Text = "评价";
                lvw_background.Columns.AddRange(new ColumnHeader[] { h1, h2, h3, h4 });
                lvw_background.View = View.Details;

                //添加数据项

                bgSites.Clear();

                AutoCompleteStringCollection names = new AutoCompleteStringCollection();

                foreach (Grogshop site in grogshopBll.Grogshops)
                {
                    names.Add(site.Name);

                    ListViewItem item = lvw_background.Items.Add(site.ID.ToString());
                    item.SubItems.Add(site.Name);
                    item.SubItems.Add("(" + site.X + "," + site.Y + ")");
                    item.SubItems.Add(site.Grade.ToString());

                    bgSites.Add(site);
                }

                txt_seekdata.AutoCompleteCustomSource = names;

                preSeekStr = "";

                if (graphChanged)
                {
                    metroGraph.UpdateGraph();
                    graphChanged = false;
                }
            }
        }

        private void rdo_restaurant_CheckedChanged(object sender, EventArgs e)
        {
            if (rdo_restaurant.Checked)
            {
                //构架表头
                lvw_background.Columns.Clear();
                lvw_background.Items.Clear();
                ColumnHeader h1 = new ColumnHeader();
                h1.Text = "ID";
                h1.Width = 40;
                ColumnHeader h2 = new ColumnHeader();
                h2.Text = "名称";
                h2.Width = 80;
                ColumnHeader h3 = new ColumnHeader();
                h3.Text = "位置";
                h3.Width = 80;
                ColumnHeader h4 = new ColumnHeader();
                h4.Text = "评价";
                lvw_background.Columns.AddRange(new ColumnHeader[] { h1, h2, h3, h4 });
                lvw_background.View = View.Details;

                //添加数据项

                bgSites.Clear();

                AutoCompleteStringCollection names = new AutoCompleteStringCollection();

                foreach (Restaurant site in restaurantBll.Restaurants)
                {
                    names.Add(site.Name);

                    ListViewItem item = lvw_background.Items.Add(site.ID.ToString());
                    item.SubItems.Add(site.Name);
                    item.SubItems.Add("(" + site.X + "," + site.Y + ")");
                    item.SubItems.Add(site.Grade.ToString());

                    bgSites.Add(site);
                }

                txt_seekdata.AutoCompleteCustomSource = names;


                preSeekStr = "";


                if (graphChanged)
                {
                    metroGraph.UpdateGraph();
                    graphChanged = false;
                }
            }
        }

        private void rdo_supermarket_CheckedChanged(object sender, EventArgs e)
        {
            if (rdo_supermarket.Checked)
            {
                //构架表头
                lvw_background.Columns.Clear();
                lvw_background.Items.Clear();
                ColumnHeader h1 = new ColumnHeader();
                h1.Text = "ID";
                h1.Width = 40;
                ColumnHeader h2 = new ColumnHeader();
                h2.Text = "名称";
                h2.Width = 80;
                ColumnHeader h3 = new ColumnHeader();
                h3.Text = "位置";
                h3.Width = 80;
                ColumnHeader h4 = new ColumnHeader();
                h4.Text = "评价";
                lvw_background.Columns.AddRange(new ColumnHeader[] { h1, h2, h3, h4 });
                lvw_background.View = View.Details;

                //添加数据项

                bgSites.Clear();

                AutoCompleteStringCollection names = new AutoCompleteStringCollection();

                foreach (Supermarket site in supermarketBll.Supermarkets)
                {
                    names.Add(site.Name);

                    ListViewItem item = lvw_background.Items.Add(site.ID.ToString());
                    item.SubItems.Add(site.Name);
                    item.SubItems.Add("(" + site.X + "," + site.Y + ")");
                    item.SubItems.Add(site.Grade.ToString());

                    bgSites.Add(site);
                }

                txt_seekdata.AutoCompleteCustomSource = names;

                preSeekStr = "";

                if (graphChanged)
                {
                    metroGraph.UpdateGraph();
                    graphChanged = false;
                }
            }

        }

        private void rdo_metronode_CheckedChanged(object sender, EventArgs e)
        {
            if (rdo_metronode.Checked)
            {
                //构架表头
                lvw_background.Columns.Clear();
                lvw_background.Items.Clear();
                ColumnHeader h1 = new ColumnHeader();
                h1.Text = "ID";
                h1.Width = 40;
                ColumnHeader h2 = new ColumnHeader();
                h2.Text = "名称";
                h2.Width = 70;
                ColumnHeader h3 = new ColumnHeader();
                h3.Text = "位置";
                h3.Width = 70;
                ColumnHeader h4 = new ColumnHeader();
                h4.Text = "终点站";
                h4.Width = 60;
                ColumnHeader h5 = new ColumnHeader();
                h5.Text = "线路1";
                h5.Width = 50;
                ColumnHeader h6 = new ColumnHeader();
                h6.Text = "线路2";
                h6.Width = 50;
                lvw_background.Columns.AddRange(new ColumnHeader[] { h1, h2, h3, h4, h5, h6 });
                lvw_background.View = View.Details;

                //添加数据项

                bgSites.Clear();

                AutoCompleteStringCollection names = new AutoCompleteStringCollection();

                foreach (MetroNode site in metroGraph.Nodes)
                {
                    names.Add(site.Name);

                    ListViewItem item = lvw_background.Items.Add(site.ID.ToString());
                    item.SubItems.Add(site.Name);
                    item.SubItems.Add("(" + site.X + "," + site.Y + ")");
                    item.SubItems.Add(site.IsTerminal.ToString());
                    item.SubItems.Add(site.Lines[0].ToString());
                    item.SubItems.Add(site.Lines.Count > 1 ? site.Lines[1].ToString() : " ");

                    bgSites.Add(site);
                }

                txt_seekdata.AutoCompleteCustomSource = names;

                preSeekStr = "";

            }

        }

        private void tab_main_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (tab_main.SelectedIndex)
            {
                case 0:
                    btn_clearinfo_Click(null, null);

                    bgSites = null;
                    break;
                case 1:
                    btn_clearpath_Click(null, null);

                    bgSites = null;
                    break;
                case 2:
                    btn_clearpath_Click(null, null);
                    btn_clearinfo_Click(null, null);
                    //生成后台数据
                    bgSites = new D_LinkList<ILocatingPoint>();
                    UpdateBgLvw();
                    txt_seekdata.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    txt_seekdata.AutoCompleteMode = AutoCompleteMode.Suggest;
                    break;
            }

        }

        private void UpdateBgLvw()
        {
            rdo_grogshop_CheckedChanged(null, null);
            rdo_restaurant_CheckedChanged(null, null);
            rdo_supermarket_CheckedChanged(null, null);
            rdo_metronode_CheckedChanged(null, null);
        }

        private void btn_seek_Click(object sender, EventArgs e)
        {
            switch (cbo_seektype.Text)
            {
                case "ID":
                    int value;
                    try
                    {
                        value = int.Parse(txt_seekdata.Text);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show("输入数据类型错误");
                        return;
                    }
                    int[] array = new int[bgSites.Count];
                    for (int i = 0; i < bgSites.Count; i++)
                    {
                        array[i] = bgSites[i].ID;
                    }
                    int index = D_Search.BinarySearchIndex(array, value);
                    lvw_background.SelectedItems.Clear();
                    lvw_background.Items[index].Selected = true;
                    lvw_background.Items[index].EnsureVisible();
                    lvw_background.Items[index].Focused = true;
                    lvw_background.Select(); //激活控件
                    break;
                case "名称":
                    if (preSeekStr != txt_seekdata.Text) //如果内容更换
                    {
                        indexs = new D_LinkQueue<int>();
                        for (int i = 0; i < bgSites.Count; i++)
                        {
                            if (D_String.SubMatching(bgSites[i].Name, txt_seekdata.Text))
                            {
                                indexs.Enqueue(i);
                            }
                        }
                        if (indexs.IsEmpty())
                        {
                            MessageBox.Show("没有找到指定数据");
                            return;
                        }
                        int nameIndex = indexs.Dequeue();
                        lvw_background.SelectedItems.Clear();
                        lvw_background.Items[nameIndex].Selected = true;
                        lvw_background.Items[nameIndex].EnsureVisible();
                        lvw_background.Items[nameIndex].Focused = true;
                        lvw_background.Select(); //激活控件
                        indexs.Enqueue(nameIndex);
                        preSeekStr = txt_seekdata.Text;
                    }
                    else if (indexs != null && indexs.Count > 0) //如果多次按查询，则直接切换
                    {
                        int nameIndex = indexs.Dequeue();
                        lvw_background.SelectedItems.Clear();
                        lvw_background.Items[nameIndex].Selected = true;
                        lvw_background.Items[nameIndex].EnsureVisible();
                        lvw_background.Items[nameIndex].Focused = true;
                        lvw_background.Select(); //激活控件
                        indexs.Enqueue(nameIndex);
                    }

                    break;
            }
        }

        //添加数据
        private void btn_adddata_Click(object sender, EventArgs e)
        {

            if (rdo_grogshop.Checked)
            {
                AlterDataDlg dlg = new AlterDataDlg("添加酒店信息",new Grogshop());
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Grogshop result = dlg.ReturnValue as Grogshop;
                    grogshopBll.AddSite(result);
                }
                else
                {
                    return;
                }
            }
            else if (rdo_restaurant.Checked)
            {
                AlterDataDlg dlg = new AlterDataDlg("添加美食信息", new Restaurant());
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Restaurant result = dlg.ReturnValue as Restaurant;
                    restaurantBll.AddSite(result);
                }
                else
                {
                    return;
                }
            }
            else if (rdo_supermarket.Checked)
            {
                AlterDataDlg dlg = new AlterDataDlg("添加超市信息", new Supermarket());
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Supermarket result = dlg.ReturnValue as Supermarket;
                    supermarketBll.AddSite(result);
                }
                else
                {
                    return;
                }
            }
            else if (rdo_metronode.Checked)
            {
                AlterDataDlg dlg = new AlterDataDlg("添加站点信息", new MetroNode());
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    MetroNode result = dlg.ReturnValue as MetroNode;
                    metroGraph.AddSite(result);

                    graphChanged = true;
                }
                else
                {
                    return;
                }
            }

            UpdateBgLvw();
        }

        //更新数据
        private void btn_updatedata_Click(object sender, EventArgs e)
        {

            if (lvw_background.FocusedItem == null)
            {
                MessageBox.Show("请选择需要修改的数据项。");
            }

            int id = -1;
            try
            {
                id = int.Parse(lvw_background.FocusedItem.SubItems[0].Text);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                return;
            }

            if (rdo_grogshop.Checked)
            {
                AlterDataDlg dlg = new AlterDataDlg("更改酒店信息", grogshopBll.GetSite(id));
                if (dlg.ShowDialog() == DialogResult.OK)
                {

                    Grogshop temp = dlg.ReturnValue as Grogshop;
                    Grogshop result = new Grogshop(id,temp.Name,temp.X,temp.Y,temp.Grade);
                    grogshopBll.UpdateSite(result);
                }
                else
                {
                    return;
                }
            }
            else if (rdo_restaurant.Checked)
            {
                AlterDataDlg dlg = new AlterDataDlg("更改美食信息", restaurantBll.GetSite(id));
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Restaurant temp = dlg.ReturnValue as Restaurant;
                    Restaurant result = new Restaurant(id, temp.Name, temp.X, temp.Y, temp.Grade);
                    restaurantBll.UpdateSite(result);
                }
                else
                {
                    return;
                }
            }
            else if (rdo_supermarket.Checked)
            {
                AlterDataDlg dlg = new AlterDataDlg("更改超市信息", supermarketBll.GetSite(id));
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Supermarket temp = dlg.ReturnValue as Supermarket;
                    Supermarket result = new Supermarket(id, temp.Name, temp.X, temp.Y, temp.Grade);
                    supermarketBll.UpdateSite(result);
                }
                else
                {
                    return;
                }
            }
            else if (rdo_metronode.Checked)
            {
                AlterDataDlg dlg = new AlterDataDlg("更改站点信息", metroGraph.GetSite(id));
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    MetroNode temp = dlg.ReturnValue as MetroNode;
                    MetroNode result = new MetroNode(id, temp.Name, temp.X, temp.Y, temp.IsTerminal,temp.Lines[0],temp.Lines.Count>1?temp.Lines[1]:-1);
                    metroGraph.UpdateSite(result);

                    graphChanged = true;
                }
                else
                {
                    return;
                }
            }


            UpdateBgLvw();
        }

        //删除数据
        private void btn_deletedata_Click(object sender, EventArgs e)
        {
            if (lvw_background.FocusedItem == null)
            {
                MessageBox.Show("请选择需要修改的数据项。");
            }

            int id = -1;
            try
            {
                id = int.Parse(lvw_background.FocusedItem.SubItems[0].Text);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
                return;
            }

            if (rdo_grogshop.Checked)
            {
                grogshopBll.DeleteSite(id);
            }
            else if (rdo_restaurant.Checked)
            {
                restaurantBll.DeleteSite(id);
            }
            else if (rdo_supermarket.Checked)
            {
                supermarketBll.DeleteSite(id);
            }
            else if (rdo_metronode.Checked)
            {
                metroGraph.DeleteSite(id);

                graphChanged = true;
            }

            UpdateBgLvw();
        }

        private void lvw_background_SelectedIndexChanged(object sender, EventArgs e)
        {
            D_LinkList<int> selectedID = new D_LinkList<int>();
            D_LinkList<ILocatingPoint> targetSite = new D_LinkList<ILocatingPoint>();

            foreach (ListViewItem item in lvw_background.SelectedItems)
            {
                int id = int.Parse(item.SubItems[0].Text); //人工保证不会出异常
                selectedID.Add(id);
            }

            if (rdo_grogshop.Checked)
            {
                foreach (ILocatingPoint site in grogshopBll.Grogshops)
                {
                    foreach (int id in selectedID)
                    {
                        if (site.ID == id)
                        {
                            targetSite.Add(site);
                        }
                    }
                }
            }
            else if (rdo_restaurant.Checked)
            {
                foreach (ILocatingPoint site in restaurantBll.Restaurants)
                {
                    foreach (int id in selectedID)
                    {
                        if (site.ID == id)
                        {
                            targetSite.Add(site);
                        }
                    }
                }
            }
            else if (rdo_supermarket.Checked)
            {
                foreach (ILocatingPoint site in supermarketBll.Supermarkets)
                {
                    foreach (int id in selectedID)
                    {
                        if (site.ID == id)
                        {
                            targetSite.Add(site);
                        }
                    }
                }
            }
            else if (rdo_metronode.Checked)
            {
                foreach (ILocatingPoint site in metroGraph.Nodes)
                {
                    foreach (int id in selectedID)
                    {
                        if (site.ID == id)
                        {
                            targetSite.Add(site);
                        }
                    }
                }
            }

            metroGraphView.SetSites(targetSite);
        }

        private void lvw_background_Leave(object sender, EventArgs e)
        {
            metroGraphView.SetSites(null);
        }
    }
}
